import json

from django.core import serializers
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.shortcuts import render

# Create your views here.
from app.models import Comments, User


def index(request):
    return render(request, 'index.html')


def register(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        age = request.POST.get('age')
        try:
            User.objects.get(username=username)
            return JsonResponse({'code': 1, 'msg': '用户已存在'})
            # return HttpResponse('用户已存在！')
        except User.DoesNotExist:
            u = User()
            u.username = username
            u.password = password
            u.age = age
            u.save()
            return JsonResponse({'code': 0, 'msg': '注册成功'})
            # return HttpResponse('注册成功！')
    else:
        if 'is_login' in request.COOKIES:
            return HttpResponseRedirect('/app/index')
        return render(request, 'register.html')


def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        try:
            user = User.objects.get(username=username)
            if user.password == password:
                # Session版本
                # request.session['is_login'] = True
                # request.session['username'] = username
                # return HttpResponseRedirect('/app/index')

                # Cookie 版本
                response = HttpResponseRedirect('/app/index')
                response.set_cookie('is_login', True, 7200)
                response.set_cookie('username', username, 7200)
                return response

                # return JsonResponse({'code': 0, 'msg': '登录成功'})
            else:
                return JsonResponse({'code': 1, 'msg': '用户名或密码不正确'})
        except User.DoesNotExist:
            return JsonResponse({'code': 1, 'msg': '用户不存在'})
    else:
        if 'is_login' in request.COOKIES:
            return HttpResponseRedirect('/app/index')
        return render(request, 'login.html')


def xss(request):
    if request.method == 'POST':
        ret = request.POST.get('inp')
        return JsonResponse({'result': ret})
    else:
        params = request.GET.get('s')
        if params:
            # return JsonResponse({'result': params})
            return HttpResponse('<div>' + params + '</div')  # 直接返回用户的输入 是极可能造成用户xss攻击的
        return render(request, 'xss.html')


def comments(request):
    if request.method == 'POST':
        comment = request.POST.get('comment')
        c = Comments()
        if comment:
            c.content = comment
            c.save()
            return JsonResponse({'code': 0, 'msg': '评论成功'})
    else:
        # 方式1：拿到的是一个ValueQuerySet对象 通过values
        res = Comments.objects.values('id', 'content')
        res = json.loads(json.dumps(list(res)))
        return JsonResponse({'code': 0, 'msg': '获取评论列表成功', 'data': res})


def count(request):
    if 'count' in request.session:
        request.session['count'] += 1
        return HttpResponse('new count=%s' % request.session['count'])
    else:
        request.session['count'] = 1
        return HttpResponse('No count in session. Setting to 1')
